package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.Category;
import com.rockcent.mall.lib.dto.CategoryDto;
import com.rockcent.mall.lib.dto.MallCategoryDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by zhangjh on 2016/10/19.
 */
@Repository
public class CategoryRepositoryImpl implements CategoryRepositoryCustom{

    private static final Logger log = LoggerFactory.getLogger(CategoryRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public List<Category> getCategoryList(Long mallId, Long merchantId) {
        SQLListDoing doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM category c WHERE id IN  (SELECT pc.category_id FROM product_categories pc LEFT JOIN product p ON p.id = pc.product_id WHERE p.mall_id = :mallId AND p.merchant_id = :merchantId GROUP BY pc.category_id)");
            fields.append("c.*");
            params.put("mallId",mallId);
            params.put("merchantId",merchantId);
        };
        return doing.go(Category.class, objectMapper, jdbcTemplate, log);
    }
    @Override
    public List<Category> findMallCategories(Long mallId){
        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append("select %s from category c where c.mall_id=:mallId and c.visible = 1  ");
            sql.append("and c.parent_id is null ");
            sql.append(" order by c.display_order_id asc ");
            params.put("mallId",mallId);
            fields.append("c.*");
        };
        return doing.go(Category.class, objectMapper, jdbcTemplate, log);
    }


    @Override
    public List<CategoryDto> getCategoryByMallIdAndIsDeleteAndParentId(Long mallId, Whether isdelete, Long leave, Long parentId) {
        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append("select * from (select %s from category c where c.mall_id=:mallId and c.is_delete=:isDelete ");
            if (parentId == null ){
                sql.append("and c.parent_id is null ");
            }else {
                sql.append("and c.parent_id =:parentId ");
                params.put("parentId",parentId);
            }
            if(leave>1){
                sql.append(" order by c.visible desc) b  ORDER BY b.visible DESC,b.display_order_id DESC,b.date_created DESC");
            }else{
                sql.append(" order by c.visible desc) b  ORDER BY b.visible DESC, b.last_updated DESC");
            }

            params.put("mallId",mallId);
            params.put("isDelete",isdelete.toString());
            fields.append("c.*");
        };
        return doing.go(CategoryDto.class, objectMapper, jdbcTemplate, log);
    }
    @Override
    public List<Category> findThreeLevel(Long mall , Long categoryId){
        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append(" SELECT * FROM category WHERE parent_id IN(SELECT id FROM category c WHERE c.parent_id = :parentId AND c.mall_id = :mall )");
            params.put("parentId",categoryId);
            params.put("mall",mall);
        };
        return doing.go(Category.class, objectMapper, jdbcTemplate, log);
    }
    @Override
    public List<MallCategoryDto> findTwoLevel(Long mallId, Long categoryId){
        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append(" select c.* from category c where c.parent_id = :parentId AND c.mall_id = :mallId  ");
            params.put("parentId",categoryId);
            params.put("mallId",mallId);
        };
        return doing.go(MallCategoryDto.class, objectMapper, jdbcTemplate, log);
    }
    @Override
    public List<MallCategoryDto> findVisiableTwoLevel(Long mallId, Long parentId){
        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append(" select c.* from category c where c.visible=1 AND c.mall_id = :mallId ");
           if(parentId==null){
               sql.append(" and  c.parent_id is null");
           }else {
               sql.append(" and  c.parent_id=:parentId");
               params.put("parentId", parentId);
           }
            params.put("mallId",mallId);
        };
        return doing.go(MallCategoryDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public List<MallCategoryDto> findVisiableThreeLevel(Long mallId, Long categoryId) {
        SQLListDoing doing = (sql, fields, params, map) -> {
            sql.append(" SELECT * FROM category WHERE parent_id IN(SELECT id FROM category c WHERE c.parent_id = :parentId AND c.mall_id = :mall and c.visible=1) and visible=1");
            params.put("parentId",categoryId);
            params.put("mall",mallId);
        };
        return doing.go(MallCategoryDto.class, objectMapper, jdbcTemplate, log);
    }

}
